package de.hauck.rtmidlet.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Utils {

	public static Date convertString2Date(String str) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		if (str.indexOf("-") > 0 && str.length() >= 10) {
			String year = str.substring(0, 4);
			String month = str.substring(5, 7);
			String day = str.substring(8, 10);
			c.set(Calendar.YEAR, Integer.parseInt(year));
			c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
			c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
			date = c.getTime();
		} else if (str.length() >= 8) {
			String year = str.substring(0, 4);
			String month = str.substring(4, 6);
			String day = str.substring(6, 8);
			c.set(Calendar.YEAR, Integer.parseInt(year));
			c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
			c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
			date = c.getTime();
			// System.out.println(str + " -> " + date);
		}

		return date;
	}

	public static String convertDate2String(Date date) {
		String ret = "";
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH) + 1;
		int d = c.get(Calendar.DATE);
		ret = (y < 10 ? "0" : "") + y + (m < 10 ? "0" : "") + m
				+ (d < 10 ? "0" : "") + d;
		// System.out.println(date + " -> " + ret);
		return ret;
	}

	public static boolean[] convertString2DisplayFlags(String str) {
		boolean[] ret = new boolean[str.length()];
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == 'X')
				ret[i] = true;
			else
				ret[i] = false;
		}
		return ret;
	}

	public static String convertDisplayFlags2String(boolean[] flags) {
		String ret = "";
		for (int i = 0; i < flags.length; i++) {
			if (flags[i])
				ret += "X";
			else
				ret += " ";
		}
		return ret;
	}

	public static String[] convertVec2StringArray(Vector vec) {
		String ret[] = new String[vec.size()];
		for (int i = 0; i < vec.size(); i++) {
			ret[i] = (String) vec.elementAt(i);
		}
		return ret;
	}

	/**
	 * 
	 * @param time
	 * @param showSeconds
	 *            Wheather or not to show just the hours and minutes part, or to
	 *            show the seconds part also.
	 * @return The Date in the format: hh:mm:ss
	 */
	public static String convertCal2Time(Calendar time, boolean showSeconds) {
		String hours = Integer.toString(time.get(Calendar.HOUR_OF_DAY));
		if (hours.length() == 1) {
			hours = '0' + hours;
		}
		String minutes = Integer.toString(time.get(Calendar.MINUTE));
		if (minutes.length() == 1) {
			minutes = '0' + minutes;
		}
		if (showSeconds) {
			String seconds = Integer.toString(time.get(Calendar.SECOND));
			if (seconds.length() == 1) {
				seconds = '0' + seconds;
			}
			return hours + ":" + minutes + ":" + seconds;
		} else {
			return hours + ":" + minutes;
		}
	}

	/**
	 * Convert given date to string<br>
	 * OutputFormat: yyyymmdd_hhmm
	 * 
	 * @return The Date/Time in the format: yyyymmdd_hhmm
	 */
	public static String convertCal2DateTimeStamp(Calendar cal) {
		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		if (hour.length() == 1) {
			hour = "0" + hour;
		}
		String minute = String.valueOf(cal.get(Calendar.MINUTE));
		if (minute.length() == 1) {
			minute = "0" + minute;
		}
		String second = String.valueOf(cal.get(Calendar.SECOND));
		if (second.length() == 1) {
			second = "0" + second;
		}
		String dateStamp = day + "." + month + "." + year + " " + hour + ":"
				+ minute + ":" + second;
		return dateStamp;
	}

	public static String convertCal2YahooDateTimeStamp(Calendar cal) {
		// String year = String.valueOf(cal.get(Calendar.YEAR));
		TimeZone yahooTZ = TimeZone.getTimeZone("Etc/GMT+5");
		TimeZone myTZ = TimeZone.getTimeZone("Europe/Berlin");

		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		// System.out.println(myTZ.getRawOffset()+" "+myTZ.getRawOffset()/3600000);
		// System.out.println(cal.get(Calendar.HOUR_OF_DAY)+" "+(myTZ.getRawOffset()-yahooTZ.getRawOffset())/3600000);
		String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)
				+ (myTZ.getRawOffset() - yahooTZ.getRawOffset()) / 3600000);
		if (hour.length() == 1) {
			hour = "0" + hour;
		}
		String minute = String.valueOf(cal.get(Calendar.MINUTE));
		if (minute.length() == 1) {
			minute = "0" + minute;
		}
		// String second = String.valueOf(cal.get(Calendar.SECOND));
		// if (second.length() == 1) {
		// second = "0" + second;
		// }
		String dateStamp = day + "." + month + ". " + hour + ":" + minute;
		return dateStamp;
	}

	public final static String replace(String text, String searchString,
			String replacementString) {
		StringBuffer sBuffer = new StringBuffer();
		int pos = 0;

		while ((pos = text.indexOf(searchString)) != -1) {
			sBuffer.append(text.substring(0, pos) + replacementString);
			text = text.substring(pos + searchString.length());
		}

		sBuffer.append(text);
		return sBuffer.toString();
	}

	public final static Image rotateImage(Image image, int angle)
			throws Exception {
		if (angle == 0) {
			return image;
		} else if (angle != 180 && angle != 90 && angle != 270) {
			throw new Exception("Invalid angle");
		}

		int width = image.getWidth();
		int height = image.getHeight();

		int[] rowData = new int[width];
		int[] rotatedData = new int[width * height];

		int rotatedIndex = 0;

		for (int i = 0; i < height; i++) {
			image.getRGB(rowData, 0, width, 0, i, width, 1);

			for (int j = 0; j < width; j++) {
				rotatedIndex = angle == 90 ? (height - i - 1) + j * height
						: (angle == 270 ? i + height * (width - j - 1) : width
								* height - (i * width + j) - 1);

				rotatedData[rotatedIndex] = rowData[j];
			}
		}

		if (angle == 90 || angle == 270) {
			return Image.createRGBImage(rotatedData, height, width, true);
		} else {
			return Image.createRGBImage(rotatedData, width, height, true);
		}
	}

	public final static Image rescaleImage(Image image, int width, int height) {
		int sourceWidth = image.getWidth();
		int sourceHeight = image.getHeight();

		Image newImage = Image.createImage(width, height);
		Graphics g = newImage.getGraphics();

		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				g.setClip(x, y, 1, 1);
				int dx = x * sourceWidth / width;
				int dy = y * sourceHeight / height;
				g
						.drawImage(image, x - dx, y - dy, Graphics.LEFT
								| Graphics.TOP);
			}
		}

		return Image.createImage(newImage);
	}

//	public final static String convertTime2String(long millis) {
//		Date date = new Date(millis);
//		Calendar c = Calendar.getInstance();
//
//		return "";
//	}

	public final static boolean isNumeric(String string) {
		try {
			Double.parseDouble(string);
			return true;
		} catch (Exception e) {
			System.out.println("'" + string + "' is not a numeric");
			return false;
		}
	}
}
